package com.project.easy.common.framework.web.controller;


import com.project.easy.common.framework.web.entity.dto.DicAddReq;
import com.project.easy.common.framework.web.entity.dto.DicTreeNodeRes;
import com.project.easy.common.framework.web.entity.dto.DicUpdateReq;
import com.project.easy.common.framework.web.entity.dto.DicVerifyCodeReq;
import com.project.easy.common.framework.web.service.IFrameworkDicService;
import com.project.easy.common.log.annotation.Logging;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.constants.VersionConstant;
import  com.project.easy.common.util.dto.IdApiContextParam;
import  com.project.easy.common.util.dto.OneApiContextParam;
import  com.project.easy.common.util.dto.OneLongApiContextParam;
import  com.project.easy.common.util.dto.R;
import com.project.easy.common.framework.web.annotation.ApiValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/api/" + VersionConstant.ONE + "/zysoft/dic")
public class FrameworkDicController extends BaseController {

    @Autowired
    private IFrameworkDicService frameworkDicService;
    /**
     * 根据字典编码查询数据
     * @param oneApiContextParam 参数
     * @return 返回数据
     */
    @PostMapping("/queryByParentKey")
    @Logging
    @ApiValidator
    public R<List<Map<String, String>>> queryByParentKey(@RequestBody OneApiContextParam oneApiContextParam) {
        return R.success(frameworkDicService.queryByParentKey(oneApiContextParam.getKey()));
    }
    /**
     * 新增
     * @param dicAddReq 参数
     * @return 返回数据
     */
    @PostMapping("/add")
    @Logging
    @ApiValidator
    public R<Long> add(@RequestBody DicAddReq dicAddReq) {
        return R.success(frameworkDicService.add(dicAddReq));
    }
    /**
     * 修改
     * @param sysParamDto 参数
     * @return 返回数据
     */
    @PostMapping("/udpate")
    @Logging
    @ApiValidator
    public R<Boolean> udpate(@RequestBody DicUpdateReq sysParamDto) {
        return R.success(frameworkDicService.updateObject(sysParamDto));
    }
    /**
     * 列表
     * @param oneLongApiContextParam 参数
     * @return 返回数据
     */
    @PostMapping("/tree")
    @Logging
    @ApiValidator
    public R<List<DicTreeNodeRes>> tree(@RequestBody OneLongApiContextParam oneLongApiContextParam) {
        return R.success(frameworkDicService.tree(0L));
    }
    /**
     * 验证编码
     * @param sysParamDto 参数
     * @return 返回数据
     */
    @PostMapping("/verifyCode")
    @Logging
    @ApiValidator
    public R<Boolean> verifyCode(@RequestBody DicVerifyCodeReq sysParamDto) {
        return R.success(frameworkDicService.verifyCode(sysParamDto));
    }
    /**
     * 禁用
     * @param oneApiContextParam 参数
     * @return 返回数据
     */
    @PostMapping("/disAble")
    @Logging
    @ApiValidator
    public R<Boolean> disAble(@RequestBody OneLongApiContextParam oneApiContextParam) {
        return R.success(this.frameworkDicService.disAble(oneApiContextParam.getKey()));
    }
    /**
     * 启用
     * @param oneApiContextParam 参数
     * @return 返回数据
     */
    @PostMapping("/able")
    @Logging
    @ApiValidator
    public R<Boolean> able(@RequestBody OneLongApiContextParam oneApiContextParam) {
        return R.success(this.frameworkDicService.able(oneApiContextParam.getKey()));
    }
    /**
     * 删除
     * @param sysParamDto 参数
     * @return 返回数据
     */
    @PostMapping("/del")
    @Logging
    @ApiValidator
    public R<Boolean> del(@RequestBody IdApiContextParam sysParamDto) {
        return R.success(this.del(frameworkDicService, v -> v::setHasDel,
                CollectionUtil.parseIds(sysParamDto.getIds())));
    }

}
